
package com.ruoyi.gisconverter.kml;

import org.geotools.data.FeatureReader;
import org.geotools.kml.v22.KML;
import org.geotools.kml.v22.KMLConfiguration;
import org.geotools.util.logging.Logging;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import javax.xml.namespace.QName;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class KMLFeatureReader implements FeatureReader<SimpleFeatureType, SimpleFeature> {

    private static final Logger LOGGER = Logging.getLogger(KMLFeatureReader.class);

    private SimpleFeatureType type = null;
    private SimpleFeature f = null;
    private org.geotools.xsd.StreamingParser parser;
    private FileInputStream fis = null;

    public KMLFeatureReader(String namespace, File file, QName name) throws IOException {
        try {
            fis = new FileInputStream(file);
            parser
                    = new org.geotools.xsd.StreamingParser(
                            new KMLConfiguration(), fis, KML.Placemark);
        } catch (Exception e) {
            throw new IOException("Error processing KML file", e);
        }
        forward();
        if (f != null) {
            type = f.getType();
        }
    }

    @Override
    public SimpleFeatureType getFeatureType() {
        return type;
    }

    /**
     * Grab the next feature from the property file.
     *
     * @return feature
     * @throws NoSuchElementException Check hasNext() to avoid reading off the
     * end of the file
     */
    @Override
    public SimpleFeature next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        SimpleFeature next = f;
        forward();
        return next;
    }

    public void forward() {
        try {
            f = (SimpleFeature) parser.parse();
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     *      */
    @Override
    public boolean hasNext() {
        return f != null;
    }

    /**
     * Be sure to call close when you are finished with this reader; as it must
     * close the file it has open.
     *
     */
    @Override
    public void close() {
        try {
            fis.close();
        } catch (IOException ex) {
            Logger.getLogger(KMLFeatureReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
